<?php


namespace App\Service;


use App\Models\Bargain_Help;
use App\Models\Bargain_User;
use App\Models\BargainHelp;
use App\Models\BargainUser;

class last
{
    public static function lastDao($bargainData,$bargainUser,$command,$uid){
        $rand = rand($bargainData['bargain_min_price']*10,$bargainData['bargain_max_price']*10)/10;
        if($bargainUser['bargain_price']<=$bargainUser['price']+$rand){
            //如果是最后一刀 剩多少补多少
            $res = Bargain_Help::create([
                'uid'=>$uid,
                'bargain_id'=>$bargainUser['bargain_id'],
                'bargain_user_id'=>$bargainUser['id'],
                'price'=>$bargainUser['bargain_price']-$bargainUser['price'],
                'add_time'=>time(),
            ]);
            //最后一刀设置为砍价成功
            Bargain_User::where('command',$command)->update([
                'price'=>$res->price+$bargainUser['price'],
                'status'=>0
            ]);
            return response()->json([
                'code'=>200,
                'msg'=>'这件商品的最后一刀',
                'data'=>[]
            ]);
        }
    }



    public static function WaveAlg($count = 20, $price = 50)
    {
        //已砍价格数组
        $aleadyList = [];//已砍钱的总和
        $aleadyCut = 0;
        for ($i = 0; $i < $count; $i++) {
            //可砍的总价-已砍的金额/砍价总次数-已砍次数//剩余砍价金额/剩余砍价次数
            //每个次数最高砍价金额
            $min = ($price - $aleadyCut) / ($count - count($aleadyList));
            //最高价格*1.5
            //没有限制商品底价唯一限制砍价次数
            //单次最高价10（波浪最低价)波浪最高价15
            $max = $min * 1.5;
            //mtrand0高效率取随机数 PHP精确度不高*100/100//得出的结果是当前用户砍价金额
            $cutPrice = mt_rand($min * 100, $max * 100) / 100;
            //最后一刀的判断
            //当前循环执行了9次==砍价了9次如果与总次数–1的结果相同则是最后一刀
            if ($i == $count - 1) {
                //当前商品总价-已经砍去的价格//同时取整保留小教点后两位
                //直接将剩余未砍的价格返回 假定由最后一刀直接砍完
                $cutPrice = round(($price * 100 - $aleadyCut * 100) / 108, 2);
            }

            //已经砍的总价＋当前用户砍的价格//得出最新的已砍总价
            $aleadyCut = ($aleadyCut * 100 + $cutPrice * 100) / 100;//将当前用户的砍价金额存入记录数组中
            $aleadyList[] = $cutPrice;
        }
        //返回砍价记录
        return $aleadyList;
    }

}
